#include "qsettingsihm.h"

QSettingsIHM::QSettingsIHM(QWidget *parent)
  : QWidget(parent)
{
	ui.setupUi(this);
	connect(ui.tabWidget,SIGNAL(currentChanged(int)),this,SLOT(pageSelected(int)));
	connect(ui.refuse,SIGNAL(clicked()),this,SLOT(hideAndQuit()));
	connect(ui.accept,SIGNAL(clicked()),this,SLOT(acceptSettings()));
	
	m_pageSettings[SETTINGS_BDD_NAME] = SETTINGS_PAGE_GENERAL;
	
	m_pageSettings[SETTINGS_DESIGNATORS_EXCLUSION_NO_PART] = SETTINGS_PAGE_ANALYSE;
	m_pageSettings[SETTINGS_DESIGNATORS_EXCLUSION_COMMON_PART] = SETTINGS_PAGE_ANALYSE;
	
	m_layouts[SETTINGS_PAGE_GENERAL] = ui.settings;
	m_layouts[SETTINGS_PAGE_ANALYSE] = ui.settings_2;
	
	QHash<QString,quint8>::iterator it;
	
	for(it = m_pageSettings.begin(); it != m_pageSettings.end() ; it++)
	{
		if(m_layouts.contains(it.value()))
		{
			QEditableSettings* setting = new QEditableSettings(rawToUse(it.key()),__getSettings(it.key()));
			m_layouts[it.value()]->addWidget(setting);
		}
	}
	
	ui.tabWidget->setCurrentIndex(SETTINGS_PAGE_GENERAL);
	
}

QSettingsIHM::~QSettingsIHM()
{
	QHash<quint8,QVBoxLayout*>::iterator la;
	for(la = m_layouts.begin(); la != m_layouts.end() ; la++)
		clearLayout(la.value());
}



void QSettingsIHM::clearLayout(QVBoxLayout* layout)
{
	while(layout->count())
	{
		QWidget* widget = layout->itemAt(0)->widget();
		widget->disconnect();
		layout->removeWidget(widget);
		delete widget;
	}
}




void QSettingsIHM::acceptSettings()
{
	QHash<quint8,QVBoxLayout*>::iterator it;
	for(it = m_layouts.begin(); it != m_layouts.end() ; it++)
	{
		QVBoxLayout* layout = it.value();
		if(layout)
		{
			for(int i=0;i<layout->count();i++)
			{
				QEditableSettings* setting =(QEditableSettings*) layout->itemAt(i)->widget();
				if(setting)
					__setSettings(useToRaw(setting->title()),useToRaw(setting->value()));
			}
		}
	}
	hideAndQuit();
}


void QSettingsIHM::hideAndQuit()
{
	hide();
	emit quitSettings();
}